<?php

/**
 *
 * @author TranHuu-Entwicklung
 */
function paging($base_url, $total, $max, $offset, $arg1 = '', $arg2 = '') {
  $data = '';
  if (!isset($total) || !isset($max))
    return '';
  if (!isset($offset))
    $offset = '';
  if ($total > $max) {
    $base_url .= '/';
    if (!empty($arg1)) {
      if (is_array($arg1)) {
        foreach ($arg1 as $a) {
          if ($a !== '') {
            $base_url .= $a . '/';
          }
        }
      } else {
        $base_url .= $arg1 . '/';
      }
    }
    $base_url .= $max . '/';
    $numpage = floor(( $total / $max ) + ( $total % $max == 0 ? 0 : 1 ));
    $currentpage = $offset / $max + 1;
    if (!empty($arg2)) {
      $arg2_url = '/';
      if ($arg2 !== '') {
        if (is_array($arg2)) {
          $arg2 .= implode('/', $arg2);
        } else {
          $arg2_url .= $arg2;
        }
      }
    } else
      $arg2_url = '';
    if ($offset > 0) {

      $data .= '<a href="' . site_url($base_url . ( $offset - $max ) . $arg2_url) . '" onclick="window.location=\'' .
              site_url($base_url . ( $offset - $max ) . $arg2_url) . '\'">
                    <input type="button" class="prev_paginate_button button" value="' . lang('paging.btn.previous') . '"/></a>';
    }
    if ($numpage <= 10) {
      for ($i = 1; $i <= $numpage; $i++) {
        if ($i == $currentpage) {
          $data .= '<span class="currentPage">' . $i . '</span>';
        } else {
          $data .= '<a href="' . site_url($base_url . ( $max * ( $i - 1 ) ) . $arg2_url) .
                  '" onclick="window.location=\'' . site_url($base_url . ( $max * ( $i - 1 ) ) . $arg2_url) .
                  '\'"><input type="button" class="button" value="' . $i . '"/></a>';
        }
      }
    } else {
      for ($i = 1; $i <= 2; $i++) {
        if ($i == $currentpage) {
          $data .= '<span class="currentPage">' . $i . '</span>';
        } else {
          $data .= '<a href="' . site_url($base_url . ( $max * ( $i - 1 ) ) . $arg2_url) . '" onclick="window.location=\'' .
                  site_url($base_url . ( $max * ( $i - 1 ) ) . $arg2_url) . '\'"><input type="button" class="button" value="' . $i . '"/></a>';
        }
      }
      if ($currentpage - 3 > 2) {
        $data .= '<span>...</span>';
      }
      for ($i = max(3, $currentpage - 2); $i <= min($numpage, $currentpage + 2); $i++) {
        if ($i == $currentpage) {
          $data .= '<span class="currentPage">' . $i . '</span>';
        } else {
          $data .= '<a href="' . site_url($base_url . ( $max * ( $i - 1 ) ) . $arg2_url) . '" onclick="window.location=\'' .
                  site_url($base_url . ( $max * ( $i - 1 ) ) . $arg2_url) . '\'"><input type="button" class="button" value="' . $i . '"/></a>';
        }
      }
      if ($currentpage + 2 < $numpage - 3) {
        $data .= '<span>...</span>';
      }
      for ($i = max($currentpage + 3, $numpage - 1); $i <= $numpage; $i++) {
        if ($i == $currentpage) {
          $data .= '<span class="currentPage">' . $i . '</span>';
        } else {
          $data .= '<a href="' . site_url($base_url . ( $max * ( $i - 1 ) ) . $arg2_url) . '" onclick="window.location=\'' .
                  site_url($base_url . ( $max * ( $i - 1 ) ) . $arg2_url) . '\'"><input type="button" class="button" value="' . $i . '"/></a>';
        }
      }
    }
    if ($offset < $total - $max) {
      $data .= '<a href="' . site_url($base_url . ( $offset + $max ) . $arg2_url) .
              '" onclick="window.location=\'' . site_url($base_url . ( $offset + $max ) . $arg2_url) .
              '\'"><input type="button" class="next_paginate_button button" value="' . lang('paging.btn.next') . '"/></a>';
    }
  }
  return $data;
}